package com.biocar.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

/**
 * @author DeSen Xu
 * @date 2021-11-22 16:24
 */
public class DateUtils {
    private DateUtils() {}


    /**
     * 将字符串按照 yyyy-MM-dd 的格式解析
     * @param date 字符串日期, 若为null返回当天日期
     * @return Date类日期
     * @throws ParseException 字符串格式有误,解析失败
     */
    public static Date parseDate(String date) throws ParseException {
        if (date == null) {
            Calendar instance = Calendar.getInstance();
            instance.set(Calendar.HOUR_OF_DAY, 0);
            instance.set(Calendar.MINUTE, 0);
            instance.set(Calendar.MILLISECOND, 0);
            instance.set(Calendar.SECOND, 0);
            return instance.getTime();
        }
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.parse(date);
    }
}
